﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Helper;

namespace DLL
{
    public class UserRepo
    {
        public User Logon(string userName, string password, out bool logonSuccess)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                string hashPassword = Encrypt.Hash(password);
                
                User user = dbContext.User.Where(x => x.Username == userName && x.Password == hashPassword).FirstOrDefault();
                if (user != null)
                {
                    user.Role = user.UserType.Name;
                    logonSuccess = true;
                }
                else
                {
                    logonSuccess = false;
                }
                return user;
            }
        }

        public bool ChangePassword(string userName, string newPassword, out string hashPassword)
        {
            bool passwordChanged = false;
            hashPassword = Encrypt.Hash(newPassword);
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                User user = dbContext.User.Where(x => x.Username == userName).FirstOrDefault();
                if (user != null)
                {
                    if (user.Password != hashPassword)
                    {
                        user.Password = hashPassword;
                        dbContext.Entry(user).State = EntityState.Modified;
                        dbContext.SaveChanges();
                        passwordChanged = true;
                    }
                }
            }
            return passwordChanged;
        }

        public void AddUser(User usr)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.User.Add(usr);
                dbContext.SaveChanges();
            }
        }

        public void UpdateUser(User usr)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.Entry(usr).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public User GetUserById(int userId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.User.Find(userId);
            }
        }

        public void DeleteUser(int userId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                User usr = GetUserById(userId);
                dbContext.User.Attach(usr);
                dbContext.User.Remove(usr);
                dbContext.SaveChanges();
            }
        }

        public List<User> GetAllUsers()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.User.ToList();
            }
        }

        public List<UserType> GetAllUserTypes()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.UserType.ToList();
            }
        }
    }
}
